{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #First Unique Character in a String"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #queue #hash-table #string #counting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #队列 #哈希表 #字符串 #计数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: firstUniqChar"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #字符串中的第一个唯一字符"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定一个字符串&nbsp;<code>s</code>&nbsp;，找到 <em>它的第一个不重复的字符，并返回它的索引</em> 。如果不存在，则返回 <code>-1</code>&nbsp;。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> s = \"leetcode\"\n",
    "<strong>输出:</strong> 0\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> s = \"loveleetcode\"\n",
    "<strong>输出:</strong> 2\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> s = \"aabb\"\n",
    "<strong>输出:</strong> -1\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示:</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>s</code>&nbsp;只包含小写字母</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [first-unique-character-in-a-string](https://leetcode.cn/problems/first-unique-character-in-a-string/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [first-unique-character-in-a-string](https://leetcode.cn/problems/first-unique-character-in-a-string/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"leetcode\"', '\"loveleetcode\"', '\"aabb\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        dic={}\n",
    "        for i in s:\n",
    "            dic[i]=dic[i]+1 if i in dic else 1\n",
    "        \n",
    "        for j in range(len(s)):\n",
    "            if dic[s[j]]==1:\n",
    "                return j\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if len(s)==0:\n",
    "            return -1\n",
    "        elif len(s)==1:\n",
    "            return 0\n",
    "        ans=[]\n",
    "        for i in s:\n",
    "            if i not in ans:\n",
    "                ans.append(i)\n",
    "        for i in ans:\n",
    "            if s.count(i)==1:\n",
    "                return s.index(i);break\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s: 'str') -> 'int':\n",
    "        h = {}\n",
    "        for c in s:\n",
    "            if c in h.keys():\n",
    "                h[c] += 1\n",
    "            else:\n",
    "                h[c] = 1\n",
    "        for i in range(len(s)):\n",
    "            if h[s[i]] == 1:\n",
    "                return i\n",
    "        return -1\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s: 'str') -> 'int':\n",
    "        k=1\n",
    "        if(len(s)==1):\n",
    "            return 0\n",
    "        for i in s:\n",
    "            nn=s[k:]+s[:k-1]\n",
    "            if(i in nn):\n",
    "                k+=1\n",
    "                continue\n",
    "            else:\n",
    "                return k-1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        s_Counter = Counter(s)\n",
    "        # print(s_Counter)\n",
    "        unique_set = set()\n",
    "\n",
    "        for key, value in s_Counter.items():\n",
    "            if value == 1:\n",
    "                unique_set.add(key)\n",
    "        # print(unique_set)\n",
    "        if len(unique_set) == 0:\n",
    "            return -1\n",
    "        for index, letter in enumerate(s):\n",
    "            if letter in unique_set:\n",
    "                return index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s: str) -> int:\n",
    "        dic = {}\n",
    "        for i in s:\n",
    "            if i not in dic:\n",
    "                dic[i] = s.count(i)\n",
    "        for j in s:\n",
    "            if j in dic and dic[j] == 1:\n",
    "                return s.index(j)\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s: str) -> int:\n",
    "        index = [s.index(i) for i in s if s.find(i) == s.rfind(i)]\n",
    "        return min(index) if len(index) > 0 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        a=list(set(list(s)))\n",
    "        a.sort(key=s.index)\n",
    "        for i,j in enumerate(a):\n",
    "            if s.count(j)==1:\n",
    "                return s.find(j)\n",
    "        return -1\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        # l = len(s)\n",
    "        # if l == 0:\n",
    "        #     return -1\n",
    "        # else:\n",
    "        #     for i in range(l):\n",
    "        #         if sum(map(lambda x:x == s[i],s)) != 1:\n",
    "        #             continue\n",
    "        #         else:\n",
    "        #             return i\n",
    "        #     else:\n",
    "        #         return\n",
    "        \n",
    "        Dict = {}\n",
    "        for i in range(len(s)):\n",
    "            if s[i] not in Dict:\n",
    "                Dict[s[i]] = 1\n",
    "            else:\n",
    "                Dict[s[i]] = Dict[s[i]]+1\n",
    "        for i in range(len(s)):\n",
    "            if Dict[s[i]]==1:\n",
    "                return i\n",
    "        return -1\n",
    "\n",
    "                    \n",
    "            \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        for letter in s:\n",
    "            l = s.find(letter)\n",
    "            r = s.rfind(letter)\n",
    "            if l == r != -1:\n",
    "                return l\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        letters='abcdefghijklmnopqrstuvwxyz'\n",
    "        index=[s.index(l) for l in letters if s.count(l) == 1]\n",
    "        return min(index) if index else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        temp = list(s)\n",
    "        d = {}\n",
    "        index = 0\n",
    "        for i in s:\n",
    "            temp.remove(i)\n",
    "            if i not in d:\n",
    "                d[i] = 1\n",
    "                if i not in temp:\n",
    "                    return index\n",
    "                index += 1\n",
    "            else:\n",
    "                index += 1\n",
    "        \n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        ls = len(s)\n",
    "        if ls == 1:\n",
    "            return 0\n",
    "        temp = []\n",
    "        for i,x in enumerate(s):\n",
    "            if x not in temp and x not in s[i+1:]:\n",
    "                return i\n",
    "            else:\n",
    "                temp.append(x)\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        tmp_c=[]\n",
    "        tmp_list=[x for x in s]\n",
    "        for i  in s :\n",
    "            tmp_list.remove(i)\n",
    "            if i not in tmp_list  and i not in tmp_c:\n",
    "                return s.index(i)\n",
    "            else:\n",
    "                tmp_c.append(i)\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        tmp_c=[]\n",
    "        tmp_list=list(s)\n",
    "        for i  in s :\n",
    "            tmp_list.remove(i)\n",
    "            if i not in tmp_list  and i not in tmp_c:\n",
    "                return s.index(i)\n",
    "            else:\n",
    "                tmp_c.append(i)\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        判断字符串为空：len(s.strip()) == 0     \n",
    "        \"\"\"\n",
    "        if len(s.strip()) == 0:\n",
    "            return -1\n",
    "        s_list = []\n",
    "        index = {}\n",
    "        for i in s:\n",
    "            s_list += i \n",
    "        \n",
    "        i = 0\n",
    "        while i < len(s_list):\n",
    "            if s_list[i] in index:\n",
    "                i += 1\n",
    "                continue\n",
    "            else:\n",
    "                if s_list[i] in s_list[i+1:]:\n",
    "                    index[s_list[i]] = 1\n",
    "                else:\n",
    "                    return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if s == None:\n",
    "            return -1\n",
    "        s_list = []\n",
    "        index = {}\n",
    "        for i in s:\n",
    "            s_list += i \n",
    "        \n",
    "        i = 0\n",
    "        while i < len(s_list):\n",
    "            if s_list[i] in index:\n",
    "                i += 1\n",
    "                continue\n",
    "            else:\n",
    "                if s_list[i] in s_list[i+1:]:\n",
    "                    index[s_list[i]] = 1\n",
    "                else:\n",
    "                    return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s: 'str') -> 'int':\n",
    "        s2=list(s)\n",
    "        n=len(s2)\n",
    "        lists=[]\n",
    "        j=0\n",
    "        if n==0:\n",
    "            return -1\n",
    "        elif n==1:\n",
    "            return j\n",
    "        else:\n",
    "            while n!=1:\n",
    "                if s2[0] not in s2[1:] and s2[0] not in lists:\n",
    "                    return j\n",
    "                else:\n",
    "                    j+=1\n",
    "                    lists.append(s2[0])\n",
    "                    del s2[0]\n",
    "                    n=len(s2)\n",
    "            if s2[0] not in lists:\n",
    "                return len(s)-1\n",
    "            else:\n",
    "                return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s: 'str') -> 'int':\n",
    "        s2=list(s)\n",
    "        n=len(s2)\n",
    "        lists=[]\n",
    "        j=0\n",
    "        if n==0:\n",
    "            return -1\n",
    "        elif n==1:\n",
    "            return j\n",
    "        else:\n",
    "            while n!=1:\n",
    "                if s2[0] not in s2[1:] and s2[0] not in lists:\n",
    "                    return j\n",
    "                else:\n",
    "                    j+=1\n",
    "                    lists.append(s2[0])\n",
    "                    del s2[0]\n",
    "                    n=len(s2)\n",
    "            if s2[0] not in lists:\n",
    "                return len(s)-1\n",
    "            else:\n",
    "                return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        for i in range(len(s)):\n",
    "            if s[i] not in s[i+1:] and s[i] not in s[:i]:\n",
    "                return i\n",
    "        return -1\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        tmp_c=[]\n",
    "        tmp_list=[ord(x) for x in list(s)]\n",
    "        for i  in s :\n",
    "            tmp_list.remove(ord(i))\n",
    "            if ord(i) not in tmp_list  and ord(i) not in tmp_c:\n",
    "                return s.index(i)\n",
    "            else:\n",
    "                tmp_c.append(ord(i))\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "\n",
    "    def firstUniqChar(self, s: str) -> int:\n",
    "        # 计算字符出现次数\n",
    "        dic = {c: s.count(c) for c in set(s)}\n",
    "\n",
    "        # 找到并返回首个满足出现次数为一的字符\n",
    "        for i, c in enumerate(s):\n",
    "            if dic[c] == 1:\n",
    "                return i\n",
    "\n",
    "        return -1\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s: str) -> int:\n",
    "        if not s: return -1\n",
    "        table = collections.defaultdict(int)\n",
    "        for i, c in enumerate(s):\n",
    "            if c in table:\n",
    "                table[c] = -1\n",
    "            else:\n",
    "                table[c] = i\n",
    "        for i, c in enumerate(s):\n",
    "            if table[c] != -1:\n",
    "                return i\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s: str) -> int:\n",
    "        index_map = Counter(s)\n",
    "        for k, v in enumerate(s):\n",
    "            if index_map[v] == 1:\n",
    "                return k\n",
    "        return -1        \n",
    "   \n",
    "\n",
    "\n",
    "      \n",
    "                \n",
    "   \n",
    "\n",
    "              \n",
    "            \n",
    "\n",
    "       \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "########时间换空间\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s: str) -> int:\n",
    "      visited = {}\n",
    "      for i in s:\n",
    "        if i in visited:\n",
    "          visited[i] += 1\n",
    "        else:\n",
    "          visited[i] = 1\n",
    "      for i in range(len(s)):\n",
    "        if visited[s[i]] == 1:\n",
    "          return i\n",
    "      return -1\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s: str) -> int:\n",
    "        dct = {}\n",
    "        for ch in s:\n",
    "            dct[ch] = dct.get(ch, 0) + 1\n",
    "        for i in dct.keys():\n",
    "            if dct[i] == 1:\n",
    "                return s.find(i)\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s: str) -> int:\n",
    "        for i in range(len(s)):\n",
    "            if s.count(s[i])==1:\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s: str) -> int:\n",
    "        hash_dic = dict()\n",
    "        for i in s:\n",
    "            hash_dic[i] = hash_dic.get(i, 0) + 1\n",
    "        for k, v in hash_dic.items():\n",
    "            if v == 1:\n",
    "                return s.index(k)\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s: str) -> int:\n",
    "        d={}\n",
    "        for x in s:\n",
    "            if x not in d:\n",
    "                d[x]=1\n",
    "            else:\n",
    "                d[x]+=1\n",
    "        for i in range(len(s)):\n",
    "            if d[s[i]]==1:\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s: str) -> int:\n",
    "        dic={}\n",
    "        for i in range(len(s)):\n",
    "            dic[s[i]]=dic.get(s[i],0)+1\n",
    "        for key,value in dic.items():\n",
    "            if value==1:\n",
    "                return s.index(key)\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s: str) -> int:\n",
    "        stack=[]\n",
    "        repet=[]\n",
    "        for st in s:\n",
    "            if st not in stack:\n",
    "                stack.append(st)\n",
    "            else:\n",
    "                repet.append(st)\n",
    "        stack1=stack[:]\n",
    "        for st in stack:\n",
    "            if st in repet:\n",
    "                stack1.remove(st)\n",
    "        if stack1!=[]:\n",
    "            return s.index(stack1[0])\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s: str) -> int:\n",
    "        l = len(s)\n",
    "        for i in range(l):\n",
    "            if s.count(s[i])==1:\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s: str) -> int:\n",
    "        for i in range(0,len(s)):\n",
    "            if s.count(s[i]) == 1:\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s: str) -> int:\n",
    "        for i in s:\n",
    "            if s.find(i) == s.rfind(i):\n",
    "                return s.index(i)\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s: str) -> int:\n",
    "        d = {}\n",
    "        for each in s:\n",
    "            d[each] = d.get(each, 0) + 1\n",
    "        for i, each in enumerate(s):\n",
    "            if d[each] == 1:\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s: str) -> int:\n",
    "        dict1 = {}\n",
    "        for i in s:\n",
    "            if i not in dict1:\n",
    "                dict1[i] = 1\n",
    "            else:\n",
    "                dict1[i] += 1\n",
    "        for j in dict1:\n",
    "            if dict1[j] == 1:\n",
    "                for k in range(len(s)):\n",
    "                    if s[k] == j:\n",
    "                        return k\n",
    "        return -1\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s: str) -> int:\n",
    "        st = collections.Counter(s)\n",
    "        for i,ch in enumerate(s):\n",
    "            if st[ch]==1:\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s: str) -> int:\n",
    "        l = set(list(s))\n",
    "        d = {}\n",
    "        S = list(s)\n",
    "        for i in l:\n",
    "            d[i] = 0\n",
    "        for j in list(s):\n",
    "            d[j] = 1 + d[j]\n",
    "        for k in range(len(S)):\n",
    "            if S[k] in d and d[S[k]] == 1:\n",
    "                return k\n",
    "        else:\n",
    "            return -1\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s: str) -> int:\n",
    "        a = []\n",
    "        for i in range(len(s)):\n",
    "            if s[i] not in s[i+1:] and s[i] not in a:\n",
    "                return i\n",
    "            else:\n",
    "                a.append(s[i])\n",
    "        return -1\n",
    "  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s: str) -> int:\n",
    "        cnt = Counter(s)\n",
    "        for idx,ch in enumerate(s):\n",
    "            if cnt[ch] == 1:\n",
    "                return idx\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s: str) -> int:\n",
    "        check = dict()\n",
    "        for i, x in enumerate(s):\n",
    "            if not x in check:\n",
    "                check[x] = 1\n",
    "            else:\n",
    "                check[x] = 0\n",
    "        for i, x in enumerate(s):\n",
    "            if check[x]:\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstUniqChar(self, s: str) -> int:\n",
    "        count = {}\n",
    "        for i,c in enumerate(s):\n",
    "            if c in count:\n",
    "                count[c][0] += 1\n",
    "            else:\n",
    "                count[c] = [1,i]\n",
    "        for v in count.values():\n",
    "            if v[0]==1:\n",
    "                return v[1]\n",
    "        return -1"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
